﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZP.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZP.UI.Utils;

//对应ServiceZP07.java
namespace Baosight.ZNH.ZP.UI
{

    /// <summary>
    /// 系统模块，树形结构 
    /// </summary>
    /// Title:ZP07，ServiceZP07
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 郭淑芳
    /// Version: 1.0
    /// History:
    ///       2012-10-17  郭淑芳   [创建]
    ///
    public partial class ZP07 : EFViewCmes
    {
        private EFTreeItem item;    //记录选中的树节点
        private BaseBlock baseBlock = new BaseBlock();//构造前台是否下拉列表
        private String myMod = "";//
        public ZP07()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZP07_Loaded);
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZP07_Loaded(object sender, RoutedEventArgs e)
        {
            this.orgTree.Refresh();
        }

        /// <summary>
        /// 获取tree选中的结点对应的label
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        private Dictionary<string, string> getTreeNodeInfo(EFTreeItem item)
        {

            string label = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
            string text = item.ItemObject.GetType().GetProperty("text").GetValue(item.ItemObject, null) as string;
            string isLeaf = (item.IsLeaf == true) ? "1" : "0";

            string parentName = item.ItemObject.GetType().GetProperty("parentName").GetValue(item.ItemObject, null) as string;
            string attribute = item.ItemObject.GetType().GetProperty("attribute").GetValue(item.ItemObject, null) as string;
            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("label", label);
            args.Add("text", text);
            args.Add("isLeaf", isLeaf);
            args.Add("parentName", parentName);
            args.Add("attribute", attribute);

            return args;
        }

        /// <summary>
        /// 重新基类的发布数据方法
        /// 刷新树节点
        /// </summary>
        /// <param name="data"></param>
        private void refreshTree(String mode)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value = "";
            myMod = mode;
            if (item != null)
            {
                if (item.IsLeaf || mode.Equals("update") || mode.Equals("delete") || mode.Equals("stop") || mode.Equals("start"))
                {
                    _value = item.ItemObject.GetType().GetProperty("text").GetValue(item.ItemObject, null) as string;
                    if (_value.Equals("root00"))
                    {
                        args.Add("node", _value);
                    }
                    else
                    {
                        _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("text").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                        args.Add("node", _value);
                    }
                }
                else
                {
                    _value = item.ItemObject.GetType().GetProperty("text").GetValue(item.ItemObject, null) as string;
                    args.Add("node", _value);
                }
            }
            BaseTools.RefreshTree("ZP07", "query", args, OnDoWorkCompleted);
        }

        /// <summary>
        /// OnDoWorkCompleted回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.orgTree.EIDataContext = new Platform.Core.UI.EIDataContext();
            this.orgTree.EIDataContext.Update(e.EIInfo);
            item.IsLoadOnDemandEnabled = true;
            if (item.IsLeaf || myMod.Equals("update") || myMod.Equals("delete") || myMod.Equals("start") || myMod.Equals("stop"))
            {
                string node = item.ItemObject.GetType().GetProperty("text").GetValue(item.ItemObject, null) as string;
                if (node.Equals("root00"))
                {
                    this.orgTree.Refresh();
                    return;
                }
                else
                {
                    EFTreeItem parent = item.Parent as EFTreeItem;
                    parent.Items.Clear();
                    parent.CollapseItem();
                    parent.ExpandItem();
                    item = BaseTools.FindTreeItem(parent.Items, node, "text") as EFTreeItem;
                    if (item == null)//如果为删除，则选中上级节点
                    {
                        this.orgTree.SelectedItem = parent;
                        Dictionary<string, string> args = getTreeNodeInfo(parent);
                        this.container.LoadView<EFCustomViewContainerPanel>("ZP08", args);
                        
                    }
                    else
                    {
                        this.orgTree.SelectedItem = item;
                        Dictionary<string, string> args = getTreeNodeInfo(item);
                        if (myMod.Equals("stop"))
                        {
                            this.container.Content = null;
                        }
                        else
                        {
                            this.container.LoadView<EFCustomViewContainerPanel>("ZP08", args);
                        }
                    }
                    
                    return;
                }
            }
            item.Items.Clear();
            item.CollapseItem();
            item.ExpandItem();
        }

        /// <summary>
        /// 第一次进入页面时选中树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void orgTree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(treeItem);
            string label = args["text"];

            if (label.Equals("root00"))
            {
                this.orgTree.SelectedItem = treeItem;//选中
                treeItem.ExpandItem();//展开
            }
        }


        /// <summary>
        /// 选中树节点，导航右侧的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void orgTree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            item = this.orgTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(item);
            string attribute = args["attribute"];

            if (attribute.Equals("1"))//组织机构停用
            {
                this.container.Content = null;
            }
            else
            {
                this.container.LoadView<EFCustomViewContainerPanel>("ZP08", args);
            }
        }

        /// <summary>
        /// 重新基类的发布数据方法
        /// 刷新树节点
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            if (data.Get("mode") != null)
            {
                String mode = data.Get("mode").ToString();
                refreshTree(mode);
            }
        }


        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {

            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            if (this.orgTree.SelectedItem == null)
            {
                EFWindow.Alert("请选中需要操作的组织机构");
                base.TempButtonOpen(true);
            }
            else
            {
                EIInfo eiInfo = new EIInfo();
                myMod = "insert";
                this.OpenWindows(eiInfo, "机构信息-新增", "insert");
            }
        }

        /// <summary>
        /// 点击 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            EIInfo eIInfo = new EIInfo();
            eIInfo.Set(EIConstants.ServiceNameKey, "ZP07");
            eIInfo.Set(EIConstants.MethodNameKey, "delete");
            EFTreeItem treeItem = this.orgTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(treeItem);
            string name = args["text"];


            if (treeItem == null)
            {
                base.OpenEFWindowAlert("请选择需要删除的组织机构！", true);
                base.TempButtonOpen(true);
                return;
            }
            else
            {
                if (name.Equals("root00"))//不能删除组织机构根节点
                {
                    base.OpenEFWindowAlert("不能删除默认节点！", true);
                    base.TempButtonOpen(true);
                    return;
                }
                DialogParameters param = this.ModelDialogParameters("Confirm", "确定删除选中的记录吗？");
                param.Closed = OnDeleteConfirmClosed;
                EFWindow.Confirm(param);

            }
        }

        /// <summary>
        /// 删除确认，点击删除按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDeleteConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，按钮生效
                return;
            }

            if (e.DialogResult.Value)
            {
                EIInfo eIInfo = new EIInfo();
                eIInfo.Set(EIConstants.ServiceNameKey, "ZP07");
                eIInfo.Set(EIConstants.MethodNameKey, "delete");
                EFTreeItem treeItem = this.orgTree.SelectedItems[0] as EFTreeItem;
                Dictionary<string, string> args = getTreeNodeInfo(treeItem);
                string name = args["text"];

                eIInfo.Set("name", name);

                myMod = "delete";
                base.Submit(eIInfo, ThisDeleteOperationCompleted);
            }
        }

        private void ThisDeleteOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                base.OpenEFWindowAlert(e.EIInfo.Msg, true);
                base.TempButtonOpen(true);
            }
            else
            {
                refreshTree("delete");
                base.TempButtonOpen(true);
            }
        }

        /// <summary>
        /// 点击 启用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnStartButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            EFTreeItem treeItem = this.orgTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(treeItem);
            string name = args["text"];
            string label = args["label"];
            string attribute = args["attribute"];

            if (treeItem == null)
            {
                base.OpenEFWindowAlert("请选择需要启用的机构！", true);
                base.TempButtonOpen(true);
                return;
            }
            else
            {
                if (name.Equals("root00"))//根节点默认为启用
                {
                    base.OpenEFWindowAlert(label + "不允许进行操作！", true);
                    base.TempButtonOpen(true);
                    return;
                }
                if (attribute.Equals("0"))//机构已经是启用状态的不能再进行启用
                {
                    base.OpenEFWindowAlert("选中的机构已经是启用状态！", true);
                    base.TempButtonOpen(true);
                    return;
                }
                DialogParameters param = this.ModelDialogParameters("Confirm", "确定启用该机构吗？");
                param.Closed = OnStartConfirmClosed;
                EFWindow.Confirm(param);

            }
        }

        /// <summary>
        /// 修改确认，点击修改按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnStartConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，按钮生效
                return;
            }

            if (e.DialogResult.Value)
            {
                EIInfo eIInfo = new EIInfo();
                eIInfo.Set(EIConstants.ServiceNameKey, "ZP07");
                eIInfo.Set(EIConstants.MethodNameKey, "start");
                EFTreeItem treeItem = this.orgTree.SelectedItems[0] as EFTreeItem;
                Dictionary<string, string> args = getTreeNodeInfo(treeItem);
                string name = args["text"];

                EIBlock block = new EIBlock("result");
                EIRow row = new EIRow();
                EIColumn Col = new EIColumn();
                Col.SetName("name");
                row.Add("name", name);
                block.AddColumn(Col);
                block.AddRow(row);
                eIInfo.AddBlock(block);
                base.Submit(eIInfo, ThisStartOperationCompleted);
            }
        }

        private void ThisStartOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                base.OpenEFWindowAlert(e.EIInfo.Msg, true);
                base.TempButtonOpen(true);
            }
            else
            {
                refreshTree("start");
               
                base.TempButtonOpen(true);
            }
        }

        /// <summary>
        /// 点击 停用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnStopButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            EFTreeItem treeItem = this.orgTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(treeItem);
            string name = args["text"];
            string label = args["label"];
            string attribute = args["attribute"];

            if (treeItem == null)
            {
                base.OpenEFWindowAlert("请选择需要停用的记录！", true);
                base.TempButtonOpen(true);
                return;
            }
            else
            {
                if (name.Equals("root00"))//根节点不能停用
                {
                    base.OpenEFWindowAlert(label + "不能进行操作！", true);
                    base.TempButtonOpen(true);
                    return;
                }
                if (attribute.Equals("1"))//已经停用的机构不能再次停用
                {
                    base.OpenEFWindowAlert("选中的机构已经是停用状态！", true);
                    base.TempButtonOpen(true);
                    return;
                }
                DialogParameters param = this.ModelDialogParameters("Confirm", "确定停用该机构吗？");
                param.Closed = OnStopConfirmClosed;
                EFWindow.Confirm(param);

            }
        }

        /// <summary>
        /// 停用确认，点击停用按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnStopConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，按钮生效
                return;
            }

            if (e.DialogResult.Value)
            {
                EIInfo eIInfo = new EIInfo();
                eIInfo.Set(EIConstants.ServiceNameKey, "ZP07");
                eIInfo.Set(EIConstants.MethodNameKey, "stop");
                EFTreeItem treeItem = this.orgTree.SelectedItems[0] as EFTreeItem;
                Dictionary<string, string> args = getTreeNodeInfo(treeItem);
                string name = args["text"];


                EIBlock block = new EIBlock("result");
                EIRow row = new EIRow();
                EIColumn Col = new EIColumn();
                Col.SetName("name");
                row.Add("name", name);
                block.AddColumn(Col);
                block.AddRow(row);
                eIInfo.AddBlock(block);
                base.Submit(eIInfo, ThisStopOperationCompleted);
            }
        }

        private void ThisStopOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                base.OpenEFWindowAlert(e.EIInfo.Msg, true);
                base.TempButtonOpen(true);
            }
            else
            {
                refreshTree("stop");
                base.TempButtonOpen(true);
            }
        }
        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public void OpenWindows(EIInfo eIInfo, string title, string dialogType)
        {

            item = this.orgTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(item);

            AddOrg addOrg = new AddOrg(dialogType);//构造一个新的修改页面
            addOrg.EIDataContext = new Platform.Core.UI.EIDataContext();
            eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.zp.orgType"));
            eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.zp.orgProperty"));
            eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.zp.business"));
            eIInfo.AddBlock(baseBlock.GetScDepartmentBlock());
            addOrg.EIDataContext.Update(eIInfo);//数据渲染
            if (dialogType.Equals("insert"))
            {
                addOrg.parentName.Value = args["text"];
            }
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = addOrg;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Tree区域
                refreshTree("insert");
            }
            //按钮生效
            base.TempButtonOpen(true);
        }
    }
}
